<?php


namespace app\admin\controller;

use app\admin\model\AdminNode;
use think\Controller;
use think\Db;
use think\Request;

class Node extends Controller {


    protected $middleware = [
        "CheckPermission",
        "Log"
    ];
    private $nodeModel;

    public function initialize()
    {
        parent::initialize();
        $this->nodeModel = new AdminNode();
    }


    public function index(Request $request){

        $nodes = $this->nodeModel->getAll();

        foreach ($nodes as &$rows){
            $con = Db::table('admin_node')->where('parent_id','=',$rows['id'])->count();
            if($con > 0){
                $rows['open'] = true;
            }

        }

        return $this->view->fetch('index',['nodes'=>json_encode($nodes)]);
    }

    // 添加节点
    public function add(Request $request){

        if($request->isPost()){

            $params = $request->post();
            $node_id = $this->nodeModel->insertOne($params);

            if($node_id) {

                $res['err'] = 0;
                $res['msg'] = '节点添加成功';
                return response($res,200,[],'json');
            } else {

                $res['err'] = 1002;
                $res['msg'] = '节点添加成功';
                return response($res,200,[],'json');
            }

        } else {

            $node_id = $request->get('node_id',0);
            $nodeInfo = [];
            if($node_id > 0){
                $nodeInfo = $this->nodeModel->getOne($node_id);
            }

            return $this->view->fetch('add',['node'=>$nodeInfo]);
        }

    }

    // 删除
    public function del(Request $request){
        if($request->isGet()){
            $node_id = $request->get('node_id',0);

            $rt = $this->nodeModel->delOne($node_id);

            if($rt){
                $res['err'] = 0;
                $res['msg'] = '节点删除成功';
                return response($res,200,[],'json');

            } else {

                $res['err'] = 1002;
                $res['msg'] = '节点删除失败';
                return response($res,200,[],'json');
            }

        } else {
            $res['err'] = 1001;
            $res['msg'] = '非法请求';
            return response($res,200,[],'json');
        }

    }

    // 编辑节点菜单
    public function edit(Request $request){

        if($request->isPost()){
            $params = $request->post();
            $node_id = $request->post('node_id',0);

            unset($params['node_id']);
            $rt = $this->nodeModel->updateOne($node_id,$params);
            if($rt){

                $res['err'] = 0;
                $res['msg'] = '节点编辑成功';
                return response($res,200,[],'json');
            } else {

                $res['err'] = 1002;
                $res['msg'] = '节点编辑失败';
                return response($res,200,[],'json');
            }

        } else {

            $node_id = $request->get('node_id',0);
            $nodeInfo = [];
            if($node_id > 0){
                $nodeInfo = $this->nodeModel->getOne($node_id);
            }
            return $this->view->fetch('edit',['node'=>$nodeInfo]);
        }

    }


}